
function Chart(opt){
	this.opt = opt || {};
	this.uid = this.opt.uid || "#tlist";
}

Chart.prototype = {
	setData: function(){

	},
	getDate: function(){

	}
}

function ParseObj(){
	// var o = {},
	// 	tmp1 = []; 
	// 	tmp2 = []; 
	// 	tmp3 = []; 
	// $("#tlist").find("tr:gt(0)").each(function(j){
	// 	tmp1[j] = $(this).find("td").eq(1).text();
	// 	tmp2[j] = $(this).find("td").eq(2).text();
	// 	tmp3[j] = $(this).find("td").eq(3).text();
	// });
	// o.preson = tmp1;
	// o.lunch = tmp2
	// o.price = tmp3;
	// return o;
}
function isAdminIp(ipArr){
	for(var i = 0; i < ipArr.length; i++){
		if(window.location.host == ipArr[i]){
			return true;
		}
	}
}

var allCookies = {
  getItem: function (sKey) {
    if (!sKey || !this.hasItem(sKey)) { return null; }
    return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
  },
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return; }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Tue, 19 Jan 2038 03:14:07 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toGMTString();
          break;
      }
    }
    document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
  },
  removeItem: function (sKey, sPath) {
    if (!sKey || !this.hasItem(sKey)) { return; }
    document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sPath ? "; path=" + sPath : "");
  },
  hasItem: function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  },
  keys: function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = unescape(aKeys[nIdx]); }
    return aKeys;
  }
};

//通用执行方法
function tableChange(){
	var total = 0;
	var isPay = [], isDel = [];
	$("#tlist").find("tr:even").css({
		"background":"#eee"
	})
	$("#tlist").find("tr:gt(0)").each(function(i){
		total += parseInt($(this).find("td").eq(3).text());
		isPay[i] = $(this).find("td").eq(4).html();
		isDel[i] = $(this).find("td").eq(5).html();
		$(this).find("td").eq(4).html($(isPay[i]).find("option:selected").html());
		$(this).find("td").eq(5).html("无权限操作");
		if($(this).find("td").eq(4).html() == "否"){
			$(this).css("background", "#FCD209");
		}
	});
	$("#tlist").after('<div style="text-align:right;height:50px;line-height:50px;font-size:20px;">总价：&yen;' + total +'</div>');
	
	if(localStorage.getItem("key") == "admin" || document.cookie.indexOf("isadmin") >= 0){
		$("#tlist").find("tr:gt(0)").each(function(i){
			$(this).find("td").eq(4).html(isPay[i]);
			$(this).find("td").eq(5).html(isDel[i]);
			$("#tlist tbody tr td").find("select").change(function() {
		        var id = $(this).attr("id");
		        var ispay = $(this).val();
		        var data = {
		            id: id,
		            ispay: ispay
		        };
		        $.ajax({
		            type: 'POST',
		            data: data,
		            dataType: 'text',
		            url: 'http://10.21.145.41/lunch/index/updatePayState',
		            error: function(XMLHttpRequest, textStatus, errorThrown) {
		                alert("error");
		            },
		            success: function(msg) {
		                alert(msg);
		                return;
		            }
		        })
		    });
		})
	}
}

$(function(){
	tableChange()
})